{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "from urllib.request import urlopen, urlretrieve\n",
    "from tqdm import tqdm\n",
    "import re\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|███████████████████████████████| 200/200 [05:56<00:00,  1.84s/it]\n",
      "100%|███████████████████████████████| 200/200 [05:38<00:00,  1.69s/it]\n",
      "100%|███████████████████████████████| 200/200 [06:16<00:00,  1.82s/it]\n",
      "100%|███████████████████████████████| 200/200 [06:00<00:00,  1.76s/it]\n",
      "100%|███████████████████████████████| 200/200 [06:46<00:00,  2.47s/it]\n",
      "100%|███████████████████████████████| 200/200 [09:04<00:00,  2.60s/it]\n",
      "100%|███████████████████████████████| 200/200 [10:12<00:00,  2.87s/it]\n",
      "100%|███████████████████████████████| 200/200 [09:01<00:00,  2.63s/it]\n",
      "100%|███████████████████████████████| 200/200 [09:39<00:00,  3.47s/it]\n",
      "100%|███████████████████████████████| 200/200 [10:56<00:00,  3.04s/it]\n",
      "100%|███████████████████████████████| 200/200 [11:12<00:00,  3.06s/it]\n",
      "100%|███████████████████████████████| 200/200 [07:46<00:00,  2.32s/it]\n",
      "100%|███████████████████████████████| 200/200 [09:30<00:00,  2.83s/it]\n",
      "100%|███████████████████████████████| 200/200 [10:05<00:00,  3.83s/it]\n"
     ]
    }
   ],
   "source": [
    "prefix = 'https://tspace.library.utoronto.ca'\n",
    "save_dir = './data/'\n",
    "if not os.path.exists(save_dir):\n",
    "    os.makedirs(save_dir)\n",
    "\n",
    "base_url = 'https://tspace.library.utoronto.ca/handle/1807/24'\n",
    "urls = [base_url+str(i) for i in range(488, 502)]\n",
    "for url in urls:\n",
    "    soup = BeautifulSoup(urlopen(url).read(), 'html5lib')\n",
    "    targets = soup.findAll('a', href=re.compile(r'/bitstream/.*.wav'))\n",
    "        \n",
    "    for a in tqdm(targets, total=len(targets), ncols=70):\n",
    "        link = a['href']\n",
    "\n",
    "        audio_save_loc = save_dir + link.split('/')[-1]\n",
    "        if os.path.isfile(audio_save_loc):\n",
    "            print(\"File Already Exists\")\n",
    "        urlretrieve(prefix+a['href'], audio_save_loc)\n",
    "\n",
    "        with open(audio_save_loc.replace('.wav', '.txt'), 'w') as f:\n",
    "            f.write('say the word ' + link.split('_')[-2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
